<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Pages extends Admin_Controller {
	
	function __construct()
	{
		parent::__construct(1, 4);
		$this->load->model('settings');
	}
	
	public function index()
	{
		$vData = array(
			'pages' => $this->admin->getPages()
		);

		$data = array(
			'content' => $this->load->view('admin/pages', $vData, true),
			'addJS' => array('/js/admin/page_list.js')
		);
		
		$this->getPage($data);
	}

	function deleted()
	{
		if (!$this->user->checkRight(4))
			redirect(base_url().'admin/pages');

		$vData = array(
			'pages' => $this->admin->getPages(null, true),
			'deleted' => true,
		);

		//$vData['blogs'] = setKeys($this->admin->getBlogs(extractFieldFromDataArray($vData['pages'], 'tBlog')), 'id');
		//$vData['users'] = $this->user->getUsernamesByIds(extractFieldFromDataArray($vData['pages'], 'tUserCreated'));
		
		$data = array(
			'content' => $this->load->view('admin/pages', $vData, true),
			'addJS' => array('/js/admin/page_list.js')
		);
		$this->getPage($data);
	}
	
	function add($id = 0)
	{
		if (!($this->user->checkRight(4)))
			redirect(base_url().'admin/pages');
		
		if (sizeof($_POST))
		{
			$id = $this->admin->savePage($_POST);
			
			if (getType($id) == "boolean")
				redirect(base_url().'admin/pages');
			else
				redirect(base_url().'admin/pages/add/'.$id);
		}
		
		$this->load->helper('form');
		$this->load->model('feedback_model', 'fb');

		$vData = array(
			'categories' => $this->admin->getCategories($id),
			'blogs' => $this->admin->getBlogs(),
			'feedback' => $this->fb->getFeedback($id),
			'addStyles' => $this->admin->getAddStyles($id),
			'addScripts' => $this->admin->getAddScripts($id)
		);
		
		if ($id = abs($id))
		{
			$vData['page'] = $this->admin->getPage($id);
			if (!$vData['page'])
				redirect(base_url().'admin/pages/add');
		}

		$vData['pages'] = $this->admin->getUpLevelPages($id);

		// diePR($vData);
		
		$data = array(
			'content' => $this->load->view('admin/pages_add', $vData, true),
			'addJS' => array(
				'/js/admin/add_page.js',
				'/js/markitup/jquery.markitup.js',
				'/js/markitup/sets/default/set.js',
				'/js/tiny_mce/jquery.tinymce.js',
				'/js/tiny_mce/tiny_mce_settings.js',
				'/js/translit.js'
			),
			'addCSS' => array(
				'/css/admin/add_page.css',
				'/js/markitup/skins/markitup/style.css',
				'/js/markitup/sets/default/style.css'
			)
		);
		
		$this->getPage($data);
	}


	
	public function order()
	{
		if (!empty($_POST))
		{
			$this->admin->savePagesOrder($_POST);
		}

		$vData = array(
			'pages' => $this->admin->getPages()
		);
		
		$data = array(
			'content' => $this->load->view('admin/pages_order', $vData, true),
			'addJS' => array(base_url().'js/admin/page_order.js'),
			'addCSS' => array(base_url().'css/admin/page_order.css')
		);
		
		$this->getPage($data);
	}

	function manage($id = 0, $manage = 0)
	{
		if (!$this->user->checkRight(4))
			die();

		if (!($id = (int)$id) || !in_array($manage = (int)$manage, array(0,1,2,3)))
			die();
		
		$where = array(
			'id' => $id
		);

		if ($manage != 3)
			$where['dDeleted'] = 0;

		if (!$this->db->where($where)->count_all_results('page'))
			die();
		
		switch ($manage) {
			case 0:
				$upd = array(
					'dAllowed' => 0,
					'tUserAllowed' => $this->user->id
				);
				break;
			case 1:
				$upd = array(
					'dAllowed' => now(),
					'tUserAllowed' => $this->user->id
				);
				break;
			case 2:
				$upd = array(
					'dDeleted' => now(),
					'tUserDeleted' => $this->user->id
				);
				break;
			case 3:
				$upd = array(
					'dAllowed' => 0,
					'dDeleted' => 0,
					'tUserDeleted' => $this->user->id
				);
				break;
		}

		echo (int)$this->db->where('id', $id)->update('page', $upd);
	}
	
	public function visit()
	{
		$vData = array(
			'visit' => $this->admin->getVisites()
		);

		$this->load->model('settings');
		
		$data = array(
			'content' => $this->load->view('admin/visit', $vData, true),
		);
		
		$this->getPage($data);
	}
	
	public function visitOne($type = 0, $id = 0)
	{
		$vData = array(
			'visit' => $this->admin->getPageVisites($type, $id)
		);

		if (empty($vData['visit']))
			$vData['users'] = array();
		else
			$vData['users'] = $this->user->getUsernamesByIds(extractFieldFromDataArray($vData['visit'], 'tUser'));

		$data = array(
			'content' => $this->load->view('admin/pageVisites', $vData, true),
		);
		
		$this->getPage($data);
	}
}

?>